package com.cnap.utils;

import com.cnap.model.application.ApplicationDTO;
import com.cnap.model.file.MergeReq;
import com.cnap.model.schedule.engine.CommentResultForEngine;
import com.cnap.model.schedule.engine.ValidateResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
 * <类文件描述>
 *
 * @author xc
 **/
class JsonUtilTest {

    @Test
    void testJsonStrToObjectForTypeReference() {
        String json = "{\n" +
                "  \"code\": 200,\n" +
                "  \"data\": {\n" +
                "    \"results\": [\n" +
                "      {\n" +
                "        \"pass\": false,\n" +
                "        \"desc\": \"部署失败!\",\n" +
                "        \"cluster\": \"cluster1\",\n" +
                "        \"detail\": \"端口占用\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"pass\": true,\n" +
                "        \"desc\": \"部署成功\",\n" +
                "        \"cluster\": \"cluster2\",\n" +
                "        \"detail\": \"\"\n" +
                "      }\n" +
                "    ]\n" +
                "  },\n" +
                "  \"message\": \"执行成功\"\n" +
                "}";

        CommentResultForEngine<ValidateResponse> result = JsonUtil.jsonStrToObject(json,
                new TypeReference<CommentResultForEngine<ValidateResponse>>() {
                });
        Assertions.assertNotNull(result);
    }

    @Test
    void jsonStrToObjectForClass() {
        String json = "{\n" +
                "\t\"applicationName\": \"0901-1\",\n" +
                "\t\"category\": \"BIGDATA\",\n" +
                "\t\"type\": \"实时计算\",\n" +
                "\t\"version\": \"V1.2\",\n" +
                "\t\"description\": \"测试更新分类\",\n" +
                "\t\"userName\": \"admin\",\n" +
                "\t\"label\": {\n" +
                "\t\t\"objectScene\": [\n" +
                "\t\t\t\"智能运维\",\n" +
                "\t\t\t\"智能推荐\",\n" +
                "\t\t\t\"人脸识别\"\n" +
                "\t\t],\n" +
                "\t\t\"technology\": [\n" +
                "\t\t\t\"模型推理\",\n" +
                "\t\t\t\"Caffe\",\n" +
                "\t\t\t\"OpenCV\",\n" +
                "\t\t\t\"飞浆\"\n" +
                "\t\t]\n" +
                "\t},\n" +
                "\t\"fileInfo\": {\n" +
                "\t\t\"name\": \"0826-big-data.zip\",\n" +
                "\t\t\"totalSize\": 2091803564,\n" +
                "\t\t\"patchNums\": 5\n" +
                "\t}\n" +
                "}";
        ApplicationDTO applicationDTO = JsonUtil.jsonStrToObject(json, ApplicationDTO.class);
        Assertions.assertNotNull(applicationDTO);
    }

    @Test
    void testObjectToJsonStr() {
        MergeReq req = new MergeReq();
        req.setMd5("md5");
        req.setTotalSize(100);
        req.setParentId("parentId");

        String json = JsonUtil.objectToJsonStr(req);
        Assertions.assertNotNull(json);
    }

}